package com.education.expenses.facade;

import com.education.expenses.assembler.UserAssembler;
import com.education.expenses.eao.UserEAO;
import com.education.expenses.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by IntelliJ IDEA.
 * User: apolozkov
 * Date: 8/15/11
 * Time: 5:34 PM
 */
@Service("userDetailsService")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class UserDetailsFacade implements UserDetailsService {

    @Autowired
    private UserEAO userEAO;

    @Autowired
    private UserAssembler userAssembler;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException, DataAccessException {
        User user = userEAO.getUserByEmail(email);
        return user != null ? userAssembler.convertUserToUserDetails(user) : null;

    }
}
